import router from './router'
import store from './store'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { getLocal } from "@/utils/localStorage";
import './utils/phone'
const whiteList = ['/login', '/register', '/codeLogin', '/home', '/market', '/foreget', '/null', '/gift', '/Codelogin'] // no redirect whitelist
const blacklist = ['/login', '/register', '/codeLogin']

router.beforeEach(async (to, from, next) => {
    NProgress.start()

    if (to.meta.title) {
        document.title = "宽情微服务注册中心-" + to.meta.title
    }
    const hasToken = localStorage.getItem("token")
    if (hasToken) {
        if (blacklist.indexOf(to.path) !== -1) {
            next({ path: '/' })
        } else {
            const hasUser_info = store.getters.user_info && store.getters.user_info.id > 0
            if (hasUser_info) {
                // await store.dispatch('user/getUserAll')
                next()
            } else {
                try {

                    next()
                } catch (error) {
                    await store.dispatch('user/resetToken')
                    next(`/login?redirect=${to.path}`)
                }
            }
        }
    } else {
        if (whiteList.indexOf(to.path) !== -1) {
            next()
        } else {
            next(`/login?redirect=${to.path}`)
        }
    }

})
router.afterEach(() => {
    NProgress.done()
})